#include <memory>
#include <iostream>
#include <cstring>

using namespace std;

// 简单内存池
class MemoryPool {
private:
    // 内存块结构
    struct MemoryBlock {
        char* data;
        MemoryBlock* next;
        size_t size;

        MemoryBlock(size_t s) : size(s) {
            data = new char[size];
            next = nullptr;
        }

        ~MemoryBlock() {
            delete[] data;
        }
    };

    static MemoryBlock* free_list;  // 空闲块链表

public:
    // 分配内存
    static void* allocate(size_t size) {
        cout << "分配 " << size << " 字节内存" << endl;

        // 查找合适的空闲块
        MemoryBlock** curr = &free_list;
        while (*curr) {
            if ((*curr)->size >= size) {
                MemoryBlock* block = *curr;
                *curr = block->next;
                char* mem = block->data;
                delete block;
                return mem;
            }
            curr = &((*curr)->next);
        }

        // 没有合适的空闲块，创建新内存
        return new char[size];
    }

    // 释放内存
    static void deallocate(void* p, size_t size) {
        if (!p) return;
        cout << "释放 " << size << " 字节内存" << endl;

        // 创建新的内存块
        MemoryBlock* block = new MemoryBlock(size);
        memcpy(block->data, p, size);

        // 插入到空闲链表
        block->next = free_list;
        free_list = block;

        delete[] static_cast<char*>(p);
    }

    // 清理内存池
    static void cleanup() {
        while (free_list) {
            MemoryBlock* block = free_list;
            free_list = free_list->next;
            delete block;
        }
    }
};

// 初始化静态成员
MemoryPool::MemoryBlock* MemoryPool::free_list = nullptr;

// 智能指针删除器
template<typename T>
class PoolDeleter {
    size_t size;
public:
    explicit PoolDeleter(size_t n = 1) : size(n * sizeof(T)) {}

    void operator()(T* p) {
        if (p) {
            MemoryPool::deallocate(p, size);
        }
    }
};

// 内存池管理类
template<typename T>
class MemoryManager {
public:
    // 创建单个对象的智能指针
    static shared_ptr<T> create() {
        T* p = static_cast<T*>(MemoryPool::allocate(sizeof(T)));
        return shared_ptr<T>(p, PoolDeleter<T>());
    }

    // 创建数组的智能指针
    static shared_ptr<T> create_array(size_t n) {
        T* p = static_cast<T*>(MemoryPool::allocate(sizeof(T) * n));
        return shared_ptr<T>(p, PoolDeleter<T>(n));
    }
};

int main() {
    try {
        // 测试单个对象
        cout << "=== 测试单个对象 ===" << endl;
        auto ptr = MemoryManager<int>::create();
        *ptr = 42;
        cout << "值: " << *ptr << endl;

        // 测试数组
        cout << "\n=== 测试数组 ===" << endl;
        auto arr = MemoryManager<int>::create_array(3);
        for (int i = 0; i < 3; i++) {
            arr.get()[i] = i * 10;
        }
        for (int i = 0; i < 3; i++) {
            cout << "arr[" << i << "] = " << arr.get()[i] << endl;
        }

        cout << "\n=== 自动释放测试 ===" << endl;
        // 智能指针离开作用域时自动释放

        // 清理内存池
        MemoryPool::cleanup();

    }
    catch (const exception& e) {
        cerr << "错误: " << e.what() << endl;
        return 1;
    }

    return 0;
}